package cn.exrick.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.exrick.utils.ImageUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.exrick.common.exception.XmallUploadException;
import cn.exrick.pojo.common.KindEditorResult;
import cn.exrick.pojo.common.Result;
import cn.exrick.utils.QiniuUtil;
import cn.exrick.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "图片上传统一接口")
public class ImageController {

    @RequestMapping(value = "/image/imageUpload", method = RequestMethod.POST)
    @ApiOperation(value = "WebUploader图片上传")
    public Result<Object> uploadFile(@RequestParam("file") MultipartFile files,
                                     HttpServletRequest request) {

        String imagePath = null;
        ServletContext context = request.getSession().getServletContext();
        // 文件保存路径
        String fileName = QiniuUtil.renamePic(files.getOriginalFilename());
        String filePath = context.getRealPath("/upload") + File.separator
                + fileName;
        // 转存文件
        try {
            // 保存至服务器
            String iconPath = context.getRealPath("/icon") + File.separator + "watermark.png";
            File iconFile = new File(iconPath);
            if(iconFile.exists()){
                // 添加水印后保存
                ImageUtil.addWaterMark(files.getInputStream(), iconFile, filePath);
            }else{
                File file = new File((filePath));
                files.transferTo(file);
            }

            /*File file = new File((filePath));
            files.transferTo(file);*/
            imagePath = "/upload/" + fileName;
            if (imagePath.contains("error")) {
                throw new XmallUploadException("上传失败");
            }
//            // 路径为文件且不为空则进行删除
//            if (file.isFile() && file.exists()) {
//                file.delete();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil<Object>().setData(imagePath);
    }

    @RequestMapping(value = "/image/upload", method = RequestMethod.POST)
    @ApiOperation(value = "手机图片上传")
    public Result<Object> upload(@RequestParam("file") MultipartFile files,
                                 HttpServletRequest request) {

        String imagePath = null;
        ServletContext context = request.getSession().getServletContext();
        // 文件保存路径
        String fileName = QiniuUtil.renamePic(files.getOriginalFilename());
        String filePath = context.getRealPath("/upload") + File.separator
                + fileName;

        try {
            File file = new File((filePath));
            files.transferTo(file);
            imagePath = "/upload/" + fileName;
            if (imagePath.contains("error")) {
                throw new XmallUploadException("上传失败");
            }
//            // 路径为文件且不为空则进行删除
//            if (file.isFile() && file.exists()) {
//                file.delete();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil<Object>().setData(imagePath);
    }

    @RequestMapping(value = "/kindeditor/imageUpload", method = RequestMethod.POST)
    @ApiOperation(value = "KindEditor图片上传")
    public KindEditorResult kindeditor(@RequestParam("imgFile") MultipartFile files, HttpServletRequest request) {

        KindEditorResult kindEditorResult = new KindEditorResult();
        // 文件保存路径
        String filePath = request.getSession().getServletContext().getRealPath("/upload") + "\\"
                + QiniuUtil.renamePic(files.getOriginalFilename());
        //检查文件
        String message = QiniuUtil.isValidImage(request, files);
        if (!message.equals("valid")) {
            kindEditorResult.setError(1);
            kindEditorResult.setMessage(message);
            return kindEditorResult;
        }
        // 转存文件
        try {
            // 保存至服务器
            File file = new File((filePath));
            files.transferTo(file);
            //上传七牛云服务器
            String imagePath = QiniuUtil.qiniuUpload(filePath);
            if (imagePath.contains("error")) {
                kindEditorResult.setError(1);
                kindEditorResult.setMessage("上传失败");
                return kindEditorResult;
            }
            // 路径为文件且不为空则进行删除
            if (file.isFile() && file.exists()) {
                file.delete();
            }
            kindEditorResult.setError(0);
            kindEditorResult.setUrl(imagePath);
            return kindEditorResult;
        } catch (IOException e) {
            e.printStackTrace();
        }
        kindEditorResult.setError(1);
        kindEditorResult.setMessage("上传失败");
        return kindEditorResult;
    }
}
